<div class="layui-fluid layui-layout-admin">
	<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
		<legend class="title"></legend>
	</fieldset>

	<form class="layui-form" action="">
		<div class="layui-form-item">
			<label class="layui-form-label">用户名</label>
			<div class="layui-input-inline">
				<input type="text" name="username" autocomplete="off" placeholder="请输入标题" class="layui-input"
					value="">
			</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label">昵称</label>
			<div class="layui-input-inline">
				<input type="text" name="nickname" autocomplete="off" placeholder="请输入标题" class="layui-input"
					value="">
			</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label">密码</label>
			<div class="layui-input-inline">
				<input type="password" name="password" lay-verify="pass" placeholder="请输入密码" autocomplete="off"
					class="layui-input">
			</div>
			<div class="layui-form-mid layui-word-aux">请填写6到12位密码,编辑时不填则不修改密码</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label">角色</label>
			<div class="layui-input-block">
				<div id="roles"></div>
			</div>
		</div>

		<div class="layui-form-item">
			<div class="layui-input-block">
				<div class="layui-footer">
					<input type="hidden" name="id" value="0" />
					<input type="hidden" name="list_table_name" value="admin-list" />
					<button class="layui-btn" lay-submit lay-filter="LAY-send-submit">立即提交</button>
				</div>
			</div>
		</div>
	</form>
</div>

<script type="text/html" template lay-done="layui.data.sendParams(d.params)">
</script>

<script >
	layui.data.sendParams = function(params){
		console.log(params);
		layui.use(['form', 'xmSelect'], function() {
			let $ = layui.jquery,
				xmSelect = layui.xmSelect,
				router = layui.router();

			let _index = layer.load();
			$(".layui-form").attr('action', layui.setter.API_HOST + 'admin/admin/save')
			let _admin_id = params.id | 0;
			$(".title").text((_admin_id > 0 ? '编辑' : '添加') + '管理员');
			
			if (_admin_id) {
				layui.admin.req({
					url: layui.setter.API_HOST + 'admin/admin/save',
					data: {
						id: _admin_id
					},
					done: function(res) {
						console.log(res);
						layer.close(_index);
						if (res.code == 0) {
							$("input[name='username']").val(res.data.username).attr('readonly', 'readonly');
							$("input[name='nickname']").val(res.data.nickname);
							$("input[name='id']").val(res.data.id);
						} else {
							layer.msg('请求失败', {icon: 5})
						}
					}
				});
			}

			layui.admin.req({
				url: layui.setter.API_HOST + 'admin/role/xm_select_data',
				data: {
					admin_id: _admin_id
				},
				done: function(res) {
					layer.close(_index);
					//渲染多选
					xmSelect.render({
						el: '#roles',
						data: res.data.data,
						initValue: res.data.initValue,
						name: 'roles'
					});
				}
			});
		});
	}
</script>
